package com.itfreer.forestry.manage;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;

import com.itfreer.gis.server.storage.StoragePara;
import com.itfreer.utils.BaseHost;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import net.spy.memcached.MemcachedClient;

import com.itfreer.file.FilePathPara;
import com.itfreer.file.LocalFilePathPara;
import com.itfreer.jwt.JwtPara;
import com.itfreer.jwt.SystemParameter;
import com.itfreer.print.TemplateFilePara;
import com.itfreer.sso.base.services.JsonPathPara;
import com.itfreer.sso.client.SsoClientConfig;
import com.itfreer.utils.file.FileMeta;
import com.itfreer.utils.file.FilePara;

/**
 * 定义平台通用配置 将所有常需要改动的地方，全部集中在此配置文件
 */
@Configuration
public class ProConfiguration {

//	private String path = "/Volumes/work/share/data";
//	private String filePath = "http://localhost/data/";
//	private String serviceName = "http://localhost:4200";
//	private String baseUrl = "http://localhost:81/";
//	private String loginUrl = baseUrl + "static/login/index.html";

	private String path = ""; // 将文件放到nginx目录中
	private String filePath = ""; // 文件访问地址
	private String serviceName = "";
	private String baseUrl = "";
	private String loginUrl = "";

//	private String path = "/docker/nginx/html/data"; 	// 将文件放到nginx目录中
//	private String filePath = "http://106.53.97.225/data/"; 		// 文件访问地址
//	private String serviceName = "http://106.53.97.225/forestrymanage";
//	private String baseUrl = "http://106.53.97.225:81/forestrymanage/";
//	private String loginUrl = baseUrl + "static/login/index.html";

	private String appKey = "";
	private String appSecret = "";
	private String memcachedIp = "";
	private Integer memcachedPort = 11211;

	private String miniopoint="";
	private String accesskey="";
	private String secretkey="";

	/**
	 * 获取后台服务基础路径
	 *
	 * @return
	 */
	@Bean
	public BaseHost baseHost() {
		BaseHost baseHost = new BaseHost();
		baseHost.setBaseUrl(filePath);
		return baseHost;
	}

	/**
	 * 当前系统参数
	 * 
	 * @return
	 */
	@Bean(name = "systemParameter")
	public SystemParameter systemParameter() {
		SystemParameter config = new SystemParameter();
		config.setsId(appKey);
		config.setsPwd(appSecret);
		return config;
	}

	/**
	 * 文件下载地址 主要是解决文件服务器问题
	 * 
	 * @return
	 */
	@Bean
	public FilePara filePara() {
		return new FilePara() {
			@Override
			public String getDownloadUrl(FileMeta file) {
//				return baseUrl + "file/jquery/download?bucket=" +
//								file.getBucketName() + "&key=" + 
//								file.getKey();
				return filePath + "file/" + file.getBucketName() + "/" + file.getKey();
			}
		};
	}

	/**
	 * 单点登录配置
	 * 
	 * @return
	 */
	@Bean(name = "ssoClientConfig")
	public SsoClientConfig ssoClientConfig() {
		//loginUrl = baseUrl + "static/login/index.html";
		SsoClientConfig config = new SsoClientConfig();
		config.setServiceName(serviceName);
		config.setAllowAnyProxy(false);
		if(loginUrl.trim().isEmpty()) {
			loginUrl = baseUrl + "static/login/index.html";
			config.setLoginUrl(loginUrl);
			config.setLogoutRedirectUrl(loginUrl + "?service=" + serviceName);
		}
		else {
			config.setLoginUrl(loginUrl);
			config.setLogoutRedirectUrl(loginUrl);
		}
		return config;
	}

	/**
	 * jwt票据参数
	 * 
	 * @return
	 */
	@Bean
	public JwtPara jwtPara() {
		JwtPara bean = new JwtPara();
		List<String> urls = new ArrayList<>();
		urls.add(serviceName);
		urls.add("http://www.itfreer.com");
		bean.setSecurityUrls(urls);
		return bean;
	}

	/**
	 * 文件存储目录 如果文件服务器为目录存储形式
	 * 
	 * @return
	 */
	@Bean
	public FilePathPara filePathPara() {
		FilePathPara para = new FilePathPara();
		para.setFilePath(path + "/file");
		return para;
	}

	/**
	 * 本地文件服务目录
	 * 
	 * @return
	 */
	@Bean
	public LocalFilePathPara localFilePathPara() {
		LocalFilePathPara para = new LocalFilePathPara();
		para.setFilePath(path + "/file");
		return para;
	}

	/**
	 * GIS服务切片存储参数
	 *
	 * @return
	 */
	@Bean(name = "storagePara")
	public StoragePara storagePara() {
		StoragePara para = new StoragePara();
		para.setEndpoint(miniopoint);
		para.setAccessKey(accesskey);
		para.setSecretKey(secretkey);
		return para;
	}

	/**
	 * 打印模板存储目录
	 * 
	 * @return
	 */
	@Bean("printParameter")
	public TemplateFilePara printParameter() {
		TemplateFilePara para = new TemplateFilePara();
		para.setTemplatePath(path + "/print");
		return para;
	}

	/**
	 * Memcached 配置
	 *
	 * @return
	 */
	@Bean
	public MemcachedClient memcachedClient() {
		MemcachedClient bean = null;
		try {
			//
			bean = new MemcachedClient(new InetSocketAddress(memcachedIp, memcachedPort));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bean;
	}

	/**
	 * 如果是单点登录服务端 注册服务的存储目录
	 * 
	 * @return
	 */
	@Bean(name = "jsonPathPara")
	public JsonPathPara jsonPathPara() {
		JsonPathPara para = new JsonPathPara();
		para.setFilePath(path + "/service");
		return para;
	}

	public String getPath() {
		return path;
	}

	@Value("${project.path}")
	public void setPath(String path) {
		this.path = path;
	}

	public String getLoginUrl() {
		return loginUrl;
	}
	@Value("${project.loginUrl}")
	public void setLoginUrl(String loginUrl) {
		this.loginUrl = loginUrl;
	}

	public String getFilePath() {
		return filePath;
	}

	@Value("${project.filepath}")
	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public String getServiceName() {
		return serviceName;
	}

	@Value("${project.servicename}")
	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}

	public String getBaseUrl() {
		return baseUrl;
	}

	@Value("${project.baseurl}")
	public void setBaseUrl(String baseUrl) {
		this.baseUrl = baseUrl;
	}

	public String getAppKey() {
		return appKey;
	}

	@Value("${project.appkey}")
	public void setAppKey(String appKey) {
		this.appKey = appKey;
	}

	public String getAppSecret() {
		return appSecret;
	}

	@Value("${project.appsecret}")
	public void setAppSecret(String appSecret) {
		this.appSecret = appSecret;
	}


	public String getMemcachedIp() {
		return memcachedIp;
	}
	@Value("${project.memcached.ip}")
	public void setMemcachedIp(String memcachedIp) {
		this.memcachedIp = memcachedIp;
	}

	public Integer getMemcachedPort() {
		return memcachedPort;
	}
	@Value("${project.memcached.port}")
	public void setMemcachedPort(Integer memcachedPort) {
		this.memcachedPort = memcachedPort;
	}

	public String getMiniopoint() {
		return miniopoint;
	}
	@Value("${project.miniopoint}")
	public void setMiniopoint(String miniopoint) {
		this.miniopoint = miniopoint;
	}

	public String getAccesskey() {
		return accesskey;
	}
	@Value("${project.accesskey}")
	public void setAccesskey(String accesskey) {
		this.accesskey = accesskey;
	}

	public String getSecretkey() {
		return secretkey;
	}
	@Value("${project.secretkey}")
	public void setSecretkey(String secretkey) {
		this.secretkey = secretkey;
	}
}
